package cn.com.tom.dao.impl;

import cn.com.tom.bean.Contact;
import cn.com.tom.bean.ContactParams;
import cn.com.tom.bean.ContactResult;
import cn.com.tom.dao.ContactDao;
import cn.com.tom.util.Dbs;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ContactDaoImpl extends BaseDaoImpl<Contact> implements ContactDao {

    @Override
    public boolean save(Contact bean) {
        Integer id = bean.id;
        String sql = "";
        List<Object> args = new ArrayList<>();
        args.add(bean.getName());
        args.add(bean.getEmail());
        args.add(bean.getComment());
        args.add(bean.getSubject());
        args.add(bean.getAlreadyRead());

        if (id == null || id < 1) {
            sql = "INSERT INTO contact(name,email,comment,subject,already_read) VALUES(?,?,?,?,?)";
        } else {
            sql = "UPDATE contact SET name=?,email=?,comment=?,subject=?,already_read=? WHERE id=?";
            args.add(id);
        }
        return Dbs.save(sql, args.toArray());
    }

    @Override
    public Contact get(Integer id) {
        String sql = "SELECT id,created_time,name,email,comment,subject,already_read FROM contact WHERE id=?";
        return Dbs.queryForObject(sql, new BeanPropertyRowMapper<>(Contact.class), id);
    }

    @Override
    public List<Contact> list() {
        return null;
    }

    public ContactResult list(ContactParams contactParams) {

        ContactResult contactResult = new ContactResult();
        StringBuilder sql = new StringBuilder();

        StringBuilder sqlTotal = new StringBuilder();
        List<Object> args = new ArrayList<>();
        sql.append("SELECT id,created_time,name,email,comment,subject,already_read FROM contact WHERE 1=1 ");
        sqlTotal.append("SELECT COUNT(*) FROM contact WHERE 1=1 ");
        if (contactParams != null) {
            if (contactParams.beginDay != null) {
                sql.append(" AND created_time>=? ");
                sqlTotal.append(" AND created_time>=? ");
                args.add(contactParams.beginDay);
                contactResult.beginDay = contactParams.beginDay;
            }

            if (contactParams.endDay != null) {
                sql.append(" AND created_time<=? ");
                sqlTotal.append(" AND created_time<=? ");
                args.add(contactParams.endDay);
                contactResult.endDay = contactParams.endDay;
            }
            if (contactParams.getState() != null && contactParams.getState() < 2) {
                sql.append(" AND already_read=? ");
                sqlTotal.append(" AND already_read=? ");
                args.add(contactParams.getState());
                contactResult.state = contactParams.state;
            }

            String keyWord = contactParams.keyword;
            if (keyWord != null && keyWord.length() > 0) {
                sqlTotal.append(" AND ( name LIKE ? OR email LIKE ? OR comment LIKE ? OR subject LIKE ? ) ");
                sql.append(" AND ( name LIKE ? OR email LIKE ? OR comment LIKE ? OR subject LIKE ? ) ");
                args.add("%" + keyWord + "%");
                args.add("%" + keyWord + "%");
                args.add("%" + keyWord + "%");
                args.add("%" + keyWord + "%");
                contactResult.keyword = contactParams.keyword;
            }

            contactResult.totalCount = Dbs.queryForCount(sqlTotal.toString(), args.toArray());


            sql.append(" limit ?,?");
            Integer pageSize = contactParams.getPageSize();
            Integer pageNo = contactParams.getPageNo();

            if (pageNo == null) {
                pageNo = 1;
            }
            if (pageSize == null) {
                pageSize = 10;
            }

            contactResult.pageSize = pageSize;
            contactResult.pageNo = pageNo;
            contactResult.totalPages = (contactResult.totalCount + pageSize - 1) / pageSize;

            int startIndex = (pageNo - 1) * pageSize;
            args.add(startIndex);
            args.add(pageSize);
        }
        List<Contact> list = Dbs.list(sql.toString(), new BeanPropertyRowMapper<>(Contact.class), args.toArray());
        contactResult.list = list;
        return contactResult;
    }

    @Override
    public boolean read(Integer id) {
        String sql = "UPDATE contact SET already_read=1 WHERE id=?";
        return Dbs.save(sql, id);
    }
}
